package com.intelligence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligence.entity.pojo.SysRole;
import com.intelligence.entity.pojo.SysUser;
import com.intelligence.entity.result.Result;
import com.intelligence.entity.result.ResultCodeEnum;
import com.intelligence.entity.vo.SysUserVo;
import com.intelligence.service.SysRoleService;
import com.intelligence.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author : RulerWood
 * @Data : 2024/5/17 - 16:12
 */
@Slf4j
@RestController
@Tag(name = "用户管理")
@RequestMapping("/intelligence-user/user/manager")
public class UserManagerController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    /**
     * 查询所有用户
     *
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/allUsers")
    @Operation(summary = "查询所有用户")
    public Result<Page<SysUserVo>> allUsers(@RequestParam(value = "offset",defaultValue = "1") Integer offset,
                                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                            Integer roleId) {
        log.info("roleId----allUsers--->{}", roleId);
        log.info("pageSize----allUsers--->{}", pageSize);
        return Result.build(sysUserService.selectAllUsersAndRole(offset,pageSize,roleId), ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户角色列表
     * @return
     */
    @GetMapping("/roleList")
    @Operation(summary = "用户角色列表")
    public Result<List<SysRole>> roleList() {
        return Result.build(sysRoleService.list(), ResultCodeEnum.SUCCESS);
    }


    /**
     * 用户禁用
     *
     * @param id  角色id
     * @return
     */
    @GetMapping("/disable/{id}")
    @Operation(summary = "用户禁用")
    public Result<SysUser> deleteUser(@PathVariable(value = "id") Integer id) {
        return Result.build(this.sysUserService.alertTheStatus(id), ResultCodeEnum.SUCCESS);
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @PostMapping("/addUser")
    @Operation(summary = "新增用户")
    public Result addUser(@RequestBody SysUser sysUser) {
        log.info("sysUser----addUser--->{}", sysUser);
        this.sysUserService.saveNewUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


}
